﻿using System;
using System.Collections.Generic;
using System.Text;
using He.Common;
using System.Data.SQLite;
using System.Data;

namespace He.DAL
{
    public partial class wx_keyword
    {
        public readonly string TblName = SysKeys.TABLE_PREFIX + "wx_keyword";

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByArticleId(int articleId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + TblName);
            strSql.Append(" where article_id=@article_id");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@article_id", DbType.String)
			};
            parameters[0].Value = articleId;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(int id, string strValue)
        {
            DbHelperSQLite.UpdateField(TblName, id, strValue);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.wx_keyword GetModel(string keyword)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + TblName);
            strSql.Append(" where keyword=@keyword order by sort_id asc,id desc");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@keyword", DbType.String)
			};
            parameters[0].Value = keyword;

            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DsToModel(ds);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.wx_keyword GetModelByArticleId(int articleId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + TblName);
            strSql.Append(" where article_id=@article_id");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@article_id", DbType.String)
			};
            parameters[0].Value = articleId;

            Model.wx_keyword model = new Model.wx_keyword();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DsToModel(ds);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 对象转换
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public Model.wx_keyword DsToModel(DataSet ds)
        {
            Model.wx_keyword model = new Model.wx_keyword();
            if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
            {
                model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
            }
            if (ds.Tables[0].Rows[0]["keyword"] != null && ds.Tables[0].Rows[0]["keyword"].ToString() != "")
            {
                model.keyword = ds.Tables[0].Rows[0]["keyword"].ToString();
            }
            if (ds.Tables[0].Rows[0]["answer"] != null && ds.Tables[0].Rows[0]["answer"].ToString() != "")
            {
                model.answer = ds.Tables[0].Rows[0]["answer"].ToString();
            }
            if (ds.Tables[0].Rows[0]["link_url"] != null && ds.Tables[0].Rows[0]["link_url"].ToString() != "")
            {
                model.link_url = ds.Tables[0].Rows[0]["link_url"].ToString();
            }
            if (ds.Tables[0].Rows[0]["sort_id"] != null && ds.Tables[0].Rows[0]["sort_id"].ToString() != "")
            {
                model.sort_id = int.Parse(ds.Tables[0].Rows[0]["sort_id"].ToString());
            }
            if (ds.Tables[0].Rows[0]["article_id"] != null && ds.Tables[0].Rows[0]["article_id"].ToString() != "")
            {
                model.article_id = ds.Tables[0].Rows[0]["article_id"].ToString();
            }
            if (ds.Tables[0].Rows[0]["msg_type"] != null && ds.Tables[0].Rows[0]["msg_type"].ToString() != "")
            {
                model.msg_type = ds.Tables[0].Rows[0]["msg_type"].ToString();
            }
            return model;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteByArticleId(int articleId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + TblName);
            strSql.Append(" where article_id=@article_id");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@article_id", DbType.String)
			};
            parameters[0].Value = articleId;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetPageList(int pageSize, int currentPage, string strWhere, string orderFiled)
        {
            return DbHelperSQLite.GetPageList(TblName, pageSize, currentPage, "*", strWhere, orderFiled);
        }
    }
}
